package com.log.blog_backend.controller;

import com.log.blog_backend.common.Result;
import com.log.blog_backend.entity.User;
import com.log.blog_backend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/user")
@CrossOrigin
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
   public Result noPwdLogin(@RequestBody String phone){
        return userService.noPwdLogin(phone);
   }

   @GetMapping("getUserById/{userId}")
    public Result getArticleById(@PathVariable("userId") Long userId){
        User user = userService.getById(userId);
        if(user!=null){
            return Result.ok(user);
        }else {
            return Result.fail("不存在",404);
        }

    }
   @PostMapping("/uploadAvatar")
    public Result uploadAvatar(@RequestBody  MultipartFile file, HttpServletRequest httpServletRequest){
       String url= userService.uploadAvatar(file,httpServletRequest);
        return Result.ok(url);
   }
    @PostMapping("/changePassword")
    public Result changePassword(@RequestBody  String newPassword, HttpServletRequest httpServletRequest){
        return userService.changePassword(newPassword,httpServletRequest);
    }

}
